package com.phone.modules.rms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.phone.modules.pms.model.PmsProduct;
import com.phone.modules.pms.service.PmsProductService;
import com.phone.modules.rms.dto.RmsBasicResultDTO;
import com.phone.modules.rms.dto.RmsQueryDTO;
import com.phone.modules.rms.dto.RmsBatteryResultDTO;
import com.phone.modules.rms.model.RmsBatteryLife;
import com.phone.modules.rms.mapper.RmsBatteryLifeMapper;
import com.phone.modules.rms.service.RmsBatteryLifeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 电池续航排行表 服务实现类
 * </p>
 *
 * @author WangHao
 * @since 2022-04-17
 */
@Service
public class RmsBatteryLifeServiceImpl extends ServiceImpl<RmsBatteryLifeMapper, RmsBatteryLife> implements RmsBatteryLifeService {

    @Autowired
    RmsBatteryLifeMapper batteryLifeMapper;

    @Autowired
    PmsProductService productService;

    /**
     * 分页获取电池续航列表
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page listBatteryLife(String keyword, Integer pageNum, Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper<RmsBatteryLife> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            queryWrapper.lambda().like(RmsBatteryLife::getProductName, keyword);
        }
        // 根据sort，顺序排序
        queryWrapper.lambda().orderByDesc(RmsBatteryLife::getTotalHour);
        return this.page(page, queryWrapper);
    }

    /**
     * 添加电池续航
     * @param batteryLife
     * @return
     */
    @Override
    public boolean saveBatteryLife(RmsBatteryLife batteryLife) {
        QueryWrapper<PmsProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PmsProduct::getProductName, batteryLife.getProductName());
        PmsProduct product = productService.getOne(queryWrapper);
        if (product != null) {
            batteryLife.setProductId(product.getId());
        }
        return this.save(batteryLife);
    }

    /**
     * 修改电池续航信息
     * @param id
     * @param batteryLife
     * @return
     */
    @Override
    public boolean updateBatteryLife(Long id, RmsBatteryLife batteryLife) {
        batteryLife.setId(id);
        return this.updateById(batteryLife);
    }

    /**
     * 批量删除电池续航信息
     * @param ids
     * @return
     */
    @Override
    public boolean deleteBatteryLife(List<Long> ids) {
        return removeByIds(ids);
    }

    /**
     * 前台展示-检索电池续航信息
     * @param queryDTO
     * @return
     */
    @Override
    public Page searchList(RmsQueryDTO queryDTO) {
        List<RmsBatteryResultDTO> resultDTOList = batteryLifeMapper.searchList(queryDTO);
        if (Objects.equals(queryDTO.getOrder(), "tab1")) {
            // 如果是tab1，需要对五小时续航+视频播放总续航时长进行排序，逆序
            resultDTOList.sort(Comparator.comparing(RmsBatteryResultDTO::getTotalHour).reversed());
            for (RmsBatteryResultDTO resultDTO : resultDTOList) {
                resultDTO.setPer(resultDTOList.get(0).getTotalHour());
            }
        } else if (Objects.equals(queryDTO.getOrder(), "tab2")) {
            // 如果是tab2，需要对五小时重度续航测试剩余电量进行排序，逆序
            resultDTOList.sort(Comparator.comparing(RmsBatteryResultDTO::getRemainPower).reversed());
        }
        Page page = new Page(queryDTO.getPageNum(), queryDTO.getPageSize());
        page.setRecords(resultDTOList);
        page.setTotal(baseMapper.getCount(queryDTO));
        return page;
    }
}
